<template>
	<view class="wrap" :style="colorStyle">
		
		<view class="headBox" v-if="!chooseAdd">
			<view class="item" v-for="(sa,idx) in headList" :key="idx" :class="{'itemActive': headIndex == idx}" @tap="headChange(idx)">
				{{sa}}
			</view>
		</view>
		
		<!-- 银行卡 -->
		<template v-if="headIndex == 1">
			<view class="list-box" v-if="list.length">
				<block v-for="(item,index) in list" :key="index">
					<view class="list">
						<view class="choos_icon" @click="choosAddrss(item)" v-if="chooseAdd">
							选择
						</view>
						<view>
							<view class="name flex_s">
								{{item.bank_name||''}}
								<text>{{item.card_username||''}}</text>
							</view>
							<view class="span">
								<text>{{item.card_number||''}}</text>
							</view>
						</view>

						<view class="btn-box flex_sb">
							<view class="left" @click="set_default(item.id)">
								<view class="img_box">
									<image src="../static/imgs/a2.png" v-if="item.is_default"></image>
									<image src="../static/imgs/a1.png" v-else></image>
								</view>
								设为默认
							</view>
							<view class="right">
								<view class="li" @click="$util.goPage(`/pages/new_page/bank/info?id=${item.id}`)">
									<image src="../static/imgs/my-17.png"></image>
									编辑
								</view>
								<view class="li" @click="openModal(item.id)">
									<image src="../static/imgs/my-18.png"></image>
									删除
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			<emptyPage v-else></emptyPage>
		</template>
		
		<!-- 支付宝 -->
		<template v-else>
			<view class="list-box" v-if="alipayList.length">
				<block v-for="(item,index) in alipayList" :key="index">
					<view class="list">
						<view class="choos_icon" @click="choosAddrss(item)" v-if="chooseAdd">
							选择
						</view>
						<view>
							<view class="name flex_s">
								{{item.alipay_name||''}}
								<text>{{item.alipay_number||''}}</text>
							</view>
						</view>
			
						<view class="btn-box flex_sb">
							<view class="left" @click="set_aliDefault(item)">
								<view class="img_box">
									<image src="../static/imgs/a2.png" v-if="item.is_default == 1"></image>
									<image src="../static/imgs/a1.png" v-else></image>
								</view>
								设为默认
							</view>
							<view class="right">
								<view class="li" @click="$util.goPage(`/pages/new_page/bank/info?id=${item.id}&playType=${headIndex}`)">
									<image src="../static/imgs/my-17.png"></image>
									编辑
								</view>
								<view class="li" @click="openModal(item.id)">
									<image src="../static/imgs/my-18.png"></image>
									删除
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			<emptyPage v-else></emptyPage>
		</template>
		
		<view class="btn-d">
			<view class="btn" @click="auth">
				{{ headIndex == 0 ? '添加支付宝账号' : '添加银行卡'}}
			</view>
		</view>
		<modal ref="modal" content="是否确认删除?" confirmText="确定" @confirm="del"></modal>
	</view>
</template>
<script>
	import colors from "@/mixins/color";
	import {
		debounceImmediate
	} from "@/utils/validate";
	import emptyPage from "@/components/emptyPage.vue";
	import modal from "@/components/modal/modal.vue";
	import {
		card_list,
		default_set,
		card_del
	} from "@/api/new_api.js";
	import {getAlipayList,postAddAlipay} from '@/api/new_store.js'
	import {
		mapGetters
	} from "vuex";
	export default {
		components: {
			emptyPage,
			modal
		},
		mixins: [colors],
		data() {
			return {
				headList:['支付宝','银行卡'],
				headIndex:0,
				alterMain: false,
				list: [],
				delCode: '',
				chooseAdd: false,
				navigatorHeight: '',
				statusBarHeight: '',
				navTitle:'银行卡管理',
				playType:1 ,// 银行卡/支付宝
				alipayList:[]
			};
		},
		computed: mapGetters(['userInfo']),
		async onLoad(option) {
			this.getSystemInfo()
			// const that = this;
			this.headIndex = option.headIndex || 0
			if (option.chooseAdd) {
				this.chooseAdd = true
				this.navTitle="选择银行卡"
				uni.setNavigationBarTitle({
					title:"选择银行卡"
				})
			}

		},
		onShow() {
			this.card_list()
			this.getAlipayListFc()
		},
		methods: {
			auth() {
				if(this.userInfo.is_real_name == 0) {
					uni.showModal({
						// title: '提示',
						content: '尚未完成实名认证，请先完成实名，后进行回购',
						success: (res)=> {
							if (res.confirm) {
								uni.setStorage({
									key: 'verification_redirect_url',
									data: '/pages/user/index'
								})
								uni.navigateTo({
									url: '/pages/new_gold/verification/verification'
								})
							}
						}
					});
					return
				}
				this.$util.goPage(`/pages/new_page/bank/info?playType=${this.headIndex}`)
			},
			getSystemInfo() {
			
				uni.getSystemInfo({
					success: res => {
						const system = res
						this.statusBarHeight = system.statusBarHeight //状态栏高度
						//获取胶囊信息
						const menu = uni.getMenuButtonBoundingClientRect()
						// this.searchLeft = this.menu.left //搜索按钮的右偏移量
						//导航栏高度= （胶囊顶部距离-状态栏高度） x 2 + 胶囊的高度
						this.navigatorHeight = (menu.top - system.statusBarHeight) * 2 + menu.height + system
							.statusBarHeight
			
			
					}
				})
			},
			choosAddrss(item) {
				if (this.chooseAdd) {
					uni.$emit('choosAddrss', item)
					uni.navigateBack()
				}

			},
			headChange(index) {
				this.headIndex = index
				if(index == 0) {
					this.getAlipayListFc()
				}else {
					this.card_list()
				}
			},
			getAlipayListFc () {
				getAlipayList().then(res=>{
					console.log('res.data.list',res.data.list);
					this.alipayList = res.data.list
				})
			},
			card_list() {
				card_list().then((res) => {
					this.list = res.data
				})
			},
			openModal(id) {
				this.delCode = id
				this.$refs.modal.open()
			},
			del: debounceImmediate(function() {
				let that = this
				card_del({
					id: this.delCode
				}).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none",
						success() {
							that.$refs.modal.close()
							that.card_list()
							that.getAlipayListFc()
						}
					});
				}).catch((err) => {
					uni.$u.toast(err)
				})
			}),
			set_default(id) {
				let that = this
				const params = {
					id: id
				}
				default_set(params).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none",
						success() {
							that.card_list()
						}
					});
				})
			},
			// 设为默认
			set_aliDefault(item) {
				console.log('设为默认item',item);
				let that = this
				let obj = {
					"card_phone": item.card_phone,
					"alipay_name": item.alipay_name,
					"alipay_number": item.alipay_number,
					"is_default": item.alipay_number == 1 ? 0 : 1,
					"b_id": item.id
				}
				postAddAlipay(obj).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none",
						success() {
							that.getAlipayListFc()
						}
					});
				}).catch((err) => {
					uni.$u.toast(err)
				})
			}

		},
	};
</script>
<style lang="scss" scoped>
	.wrap {
		overflow: hidden;
		width: 750rpx;
		min-height: 100vh;
		position: relative;
		background-color: #F6F7FB;
		padding-bottom: 200rpx;
		
		.headBox {
			display: flex;
			align-items: center;
			justify-content: space-around;
			width: 749rpx;
			height: 100rpx;
			background: #fff;
			
			
			.item {
				position: relative;
				height: 100rpx;
				line-height: 100rpx;
				font-size: 34rpx;
				font-weight: 550;
				text-align: center;
				color: rgba(61, 61, 61, 1);				
			}
			.itemActive {
				color: rgba(195, 167, 105, 1);
			}
			.itemActive::before {
				content: '';
				position: absolute;
				right: 0;
				bottom: 0;
				width: 100rpx;
				height: 10rpx;
				background: linear-gradient(90deg, rgba(242, 226, 174, 1) 0%, rgba(249, 244, 229, 1) 47.92%, rgba(237, 222, 177, 1) 100%);
			}
			
		}

		.nav_box {

			background: #fff;

			.nav_info {
				font-size: 30rpx;
				height: 88rpx;
				flex-grow: 1;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 27rpx;

				.back_icon {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 32rpx;
					height: 32rpx;

					.iconfont {
						font-size: 26rpx;
					}
				}

				.sys_title {
					color:#383838;
					text-align: center;
				}
			}

		}

		.list-box {
			width: 700rpx;
			overflow: hidden;
			margin: 0 auto;

			.list {
				position: relative;
				width: 100%;
				background: white;
				border-radius: 8rpx;
				padding: 20rpx 30rpx;
				position: relative;
				margin-top: 20rpx;
				overflow: hidden;
				line-height: 50rpx;
				color: #3D3D3D;
				font-size: 28rpx;

				.choos_icon {
					position: absolute;
					font-size: 26rpx;
					right: 30rpx;
					top: 20rpx;
					width: 95rpx;
					height: 44rpx;
					border-radius: 44rpx;
					line-height: 44rpx;
					background: #383838;
					color: #fff;
					text-align: center;
				}

				.name {
					font-weight: 700;

					text {
						margin-left: 20rpx;
					}

					.tip_box {
						margin-left: 20rpx;
						font-size: 26rpx;
						color: #808080;
						width: 177rpx;
						text-align: center;
						border-radius: 4rpx;
						background: #EDEDED;
						height: 37rpx;
						line-height: 37rpx;
					}
				}

				.span {
					color: #757575;
					font-size: 26rpx;

					text {
						color: #3D3D3D;
						margin-right: 20rpx;
					}
				}

				.btn-box {
					border-top: solid 1rpx #F3F4F8;
					padding: 10rpx 0 0 0;
					overflow: hidden;
					width: 100%;
					margin-top: 10rpx;

					.left {
						display: flex;
						align-items: center;
						width: 40%;

						vertical-align: top;

						.img_box {
							width: 34rpx;
							height: 34rpx;
							margin-right: 10rpx;

							image {
								width: 100%;
								height: 100%;

							}
						}

					}

					.right {
						display: flex;

						vertical-align: top;

						.li {
							display: flex;
							align-items: center;
							vertical-align: top;
							margin-left: 20rpx;

							image {
								width: 34rpx;
								height: 34rpx;
								display: inline-block;
								vertical-align: top;
								margin-right: 10rpx;
							}
						}
					}
				}
			}
		}

		.btn-d {
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: white;
			padding: 30rpx;
			font-size: 32rpx;
			color: white;
			text-align: center;
			line-height: 90rpx;
			padding-bottom: 50rpx;

			.btn {
				border-radius: 50rpx;
				background: var(--view-theme);
			}
		}
	}
</style>